using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace AppManagement.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using AppManagement.Models;
	using AppManagement.Repositories;
	using AppManagement.Helpers;
	using AppManagement.UsersManager;
	using System.Data;
	using System.Drawing;
	using System.IO;
    using AppManagement.Reports;
	#endregion

	public partial class FrmChiTietPHIEUYEUCAU : Office2007Form
	{
		public EventHandler Handler { get; set; }
		private DataTable DataSource;
		private int PHIEUYEUCAU_ID;
        private int CHINHANH_ID = -1;

		public FrmChiTietPHIEUYEUCAU(int phieuyeucau_id = -1)
		{
			InitializeComponent();
			PHIEUYEUCAU_ID = phieuyeucau_id;
            txtNGAYYEUCAU.Text = String.Format("{0:dd/MM/yyyy}", DateTime.Now);
            if (GlobalVariables.g_CurrentUser.LOAINGUOIDUNG.TEN_LOAI_NGUOI_DUNG.ToUpper().Contains("NHÂN VIÊN CHI NHÁNH"))
            {
                btnLapPhieuXuat.Enabled = false;
                btnCapNhat.Enabled = false;
            }
		}

		private void FrmChiTietPHIEUYEUCAU_Load(object sender, EventArgs e)
		{
			InitGridView();
			InitHiddenFields();
			InitDataSource();
			if (PHIEUYEUCAU_ID != -1)
			{
				LoadPHIEUYEUCAU();
			}
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID Thiết bị",
				DataPropertyName = "THIET_BI_ID",
				Name = "THIET_BI_ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Tên thiết bị",
				DataPropertyName = "TEN_THIET_BI",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Số lượng",
				DataPropertyName = "SO_LUONG",
				Name = "SO_LUONG",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/4))
			};
			dtgvData.Columns.Add(objColumn);
            objColumn = new DataGridViewTextBoxColumn
            {
                HeaderText = "Đã xuất",
                DataPropertyName = "DA_XUAT",
                Name = "DA_XUAT",
                Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth) * ((double)1 / 4)) - 1
            };
            dtgvData.Columns.Add(objColumn);
		}

		public void InitHiddenFields()
		{
		}

		public void InitDataSource()
		{
			DataSource = new DataTable();
			DataSource.Columns.Add("THIET_BI_ID");
			DataSource.Columns.Add("TEN_THIET_BI");
			DataSource.Columns.Add("SO_LUONG");
            DataSource.Columns.Add("DA_XUAT");
			DataView objTimeSheetDV = new System.Data.DataView(DataSource);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}


		private void btnThem_Click(object sender, EventArgs e)
		{
			FrmCapNhatCHITIETPHIEUYEUCAU frm = new FrmCapNhatCHITIETPHIEUYEUCAU();
			frm.Handler += GetCHITIETPHIEUYEUCAU;
			frm.ShowDialog();
		}

		public void GetCHITIETPHIEUYEUCAU(object sender, EventArgs e)
		{
			var eventType = (TransferSimpleDataEvent)e;
			string[] comp = eventType.Data.Split(new char[] { '#' });
			var item = THIETBIRepository.SelectByID(int.Parse(comp[0]));
			if(item != null)
			{
				DataSource.Rows.Add(item.THIET_BI_ID, item.TEN_THIET_BI, comp[1]);
			}
		}


		private void btnXoa_Click(object sender, EventArgs e)
		{
			if (DataSource.Rows.Count > 0)
			{
				DataSource.Rows.RemoveAt(dtgvData.SelectedRows[0].Index);
			}
		}

		private bool ValidateInput(ref string errorText)
		{
            if (!ckbxDangXuLy.Checked && ckbxDaXuLy.Checked)
            {
                errorText = "Phiếu yêu cầu chưa trong quá trình xử lý";
                return false;
            }
			return true;
		}

		private void btnCapNhat_Click(object sender, EventArgs e)
		{
			var errorText = "";
			bool success = false;
			if (!ValidateInput(ref errorText))
			{
				MessageBoxEx.Show(errorText, "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
			if (ActionUpdate())
			{
				success = true;
			}
			if (success)
			{
				MessageBoxEx.Show("Cập nhật dữ liệu thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
			}
			else
			{
				MessageBoxEx.Show("Cập nhật dữ liệu thất bại", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

        private bool ActionUpdate()
        {
            try
            {
                var phieuyeucau = PHIEUYEUCAURepository.SelectByID(PHIEUYEUCAU_ID);
                phieuyeucau.DANG_XU_LY = ckbxDangXuLy.Checked;
                phieuyeucau.DA_XU_LY = ckbxDaXuLy.Checked;
                return PHIEUYEUCAURepository.Save();
            }
            catch
            {
                return false;
            }
        }

		private void btnThoat_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void LoadPHIEUYEUCAU()
		{
			var phieuyeucau = PHIEUYEUCAURepository.SelectByID(PHIEUYEUCAU_ID);
			if (phieuyeucau != null)
			{
				txtPHIEUYEUCAU_ID.Text = PHIEUYEUCAU_ID.ToString();
                txtNGAYYEUCAU.Text = String.Format("{0:dd/MM/yyyy}", phieuyeucau.NGAY_YEU_CAU.Value); 
                CHINHANH_ID = phieuyeucau.CHINHANH_ID.Value;
                txtCHINHANH.Text = phieuyeucau.CHINHANH.TEN_CHI_NHANH;
                ckbxDangXuLy.Checked = phieuyeucau.DANG_XU_LY.Value;
                ckbxDaXuLy.Checked = phieuyeucau.DA_XU_LY.Value;
                // Load chi tiet
                LoadCHITIETPHIEUYEUCAU(phieuyeucau);
			}
		}

		private void LoadCHITIETPHIEUYEUCAU(PHIEUYEUCAU phieuyeucau)
		{
			var lst = CHITIETPHIEUYEUCAURepository.SelectByPHIEUYEUCAU_ID(phieuyeucau.PHIEUYEUCAU_ID);
			if (lst.Count > 0)
			{
                DataSource.Rows.Clear();
				foreach (var item in lst)
				{
					DataSource.Rows.Add(item.THIET_BI_ID, item.THIETBI.TEN_THIET_BI, item.SO_LUONG, item.DA_XUAT);
				}
			}
		}

        private void FrmChiTietPHIEUYEUCAU_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Handler(sender, e);
        }

        private void btnLapPhieuXuat_Click(object sender, EventArgs e)
        {
            FrmLapPHIEUXUATKHO frm = new FrmLapPHIEUXUATKHO(PHIEUYEUCAU_ID);
            frm.Handler += LoadPHIEUYEUCAU;
            frm.ShowDialog();
        }

        private void btnDanhSachPhieuXuat_Click(object sender, EventArgs e)
        {
            FrmQuanLyPHIEUXUATKHO frm = new FrmQuanLyPHIEUXUATKHO(PHIEUYEUCAU_ID);
            frm.ShowDialog();
        }

        private void LoadPHIEUYEUCAU(object sender, EventArgs e)
        {
            var phieuyeucau = PHIEUYEUCAURepository.SelectByID(PHIEUYEUCAU_ID);
            if (phieuyeucau != null)
            {
                LoadCHITIETPHIEUYEUCAU(phieuyeucau);
            }
        }

        private void btnInPhieu_Click(object sender, EventArgs e)
        {
            var phieuyeucau = PHIEUYEUCAURepository.SelectByID(PHIEUYEUCAU_ID);
            FrmInPhieuYeuCau frm = new FrmInPhieuYeuCau
            {
                NgayYeuCau = phieuyeucau.NGAY_YEU_CAU.Value,
                ChiNhanh = phieuyeucau.CHINHANH.TEN_CHI_NHANH,
                IDPhieuYeuCau = phieuyeucau.PHIEUYEUCAU_ID
            };
            frm.ShowDialog();
        }
	}
}
